<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col :span="7">
          <!-- 搜索框 -->
          <el-input placeholder="请输入内容" class="input-with-select">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 订单列表 -->
      <el-table :data="list" border stripe>
         <el-table-column type="index" label="#"></el-table-column>
         <el-table-column label="订单编号" prop="order_number"></el-table-column>
         <el-table-column label="订单价格" prop="order_price"></el-table-column>
         <el-table-column label="是否付款" prop="pay_status">
            <el-tag type="danger">未付款</el-tag>
         </el-table-column>
         <el-table-column label="是否发货" prop="is_send"></el-table-column>
         <el-table-column label="下单时间" prop="create_time">
           <template v-slot="scope">
            {{scope.row.create_time | relativeTime}}
           </template>
           <!-- {{create_time | relativeTime}} -->
         </el-table-column>
         <el-table-column label="操作" prop="order_number"></el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryList.pagenum"
        :page-sizes="[5, 10, 20, 40]"
        :page-size="queryList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

    </el-card>
  </div>
</template>

<script>
import { getOrdersList } from '@/api/order'
export default {
  data () {
    return {
      queryList: {
        query: '', // 查询参数
        pagenum: 1, // 当前页码
        pagesize: 10 // 每页显示条数
      },
      total: 0, // 数据条数
      list: [] // 订单列表
    }
  },
  created () {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList () {
      try {
        const data = await getOrdersList(this.queryList)
        console.log(data)
        this.total = data.total
        this.list = data.goods
      } catch (error) {
        this.$message.error('获取列表失败')
      }
    },
    handleSizeChange (val) {
      this.queryList.pagesize = val
      this.getOrdersList()
    },
    handleCurrentChange (val) {
      this.queryList.pagenum = val
      this.getOrdersList()
    }
  },
  filters: {
    // 格式化时间
    relativeTime: function (time) {
      const data = new Date(time)
      const year = data.getFullYear()
      const moth = (data.getMonth() + 1 + '').padStart(2, '0')
      const dates = (data.getDate() + '').padStart(2, '0')

      const h = (data.getHours() + '').padStart(2, '0')
      const m = (data.getMinutes() + '').padStart(2, '0')
      const s = (data.getSeconds() + '').padStart(2, '0')

      return `${year}-${moth}-${dates} ${h}:${m}:${s}`
    }
  }
}
</script>

<style>
.el-card {
  margin-top: 20px;
}
.el-row {
  margin-bottom: 20px;
}
.pagination {
  margin-top: 20px;
}
</style>
